﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using WorkDAL.GrossTable;
using Lapas.Common;
using System.Data.SqlClient;
using Common;

namespace GrossControl
{
    public partial class GrossTableForm : BaseForm
    {
        public string GrossKey { get; set; }
        public string GrossTable { get; set; }
        public string ValueOld { get; set; }

        public GrossTableForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            txtOldValue.Text = ValueOld;
            txtOldValue.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtNewValue.Text.Trim() == ValueOld.ToString())
                    return;

                List<tbGrossTable> grossTable = GrossTableDataAccess.Instance().GrossTable_GetList(GrossKey);
                progressBar1.Maximum = grossTable.Count() + 1;
                progressBar1.Minimum = 0;
                progressBar1.Value = 0;

                
                using (SqlConnection sqlConnection = new SqlConnection(CommonSettings.ConnectionString))
                {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = new SqlCommand("sp_GrossTable", sqlConnection);
                    SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
                    sqlCommand.Transaction = sqlTransaction;
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.Add("@TableKey", SqlDbType.VarChar);
                    sqlCommand.Parameters.Add("@GrossTable", SqlDbType.VarChar);
                    sqlCommand.Parameters.Add("@ValueOld", SqlDbType.VarChar);
                    sqlCommand.Parameters.Add("@ValueNew", SqlDbType.VarChar);

                    foreach (tbGrossTable item in grossTable)
                    {
                        progressBar1.Value += 1;
                        sqlCommand.Parameters["@TableKey"].Value = item.TableKey;
                        sqlCommand.Parameters["@GrossTable"].Value = item.GrossTable;
                        sqlCommand.Parameters["@ValueOld"].Value = ValueOld;
                        sqlCommand.Parameters["@ValueNew"].Value = txtNewValue.Text.Trim();

                        sqlCommand.ExecuteNonQuery();   
                    }

                    sqlCommand.Transaction.Commit();
                    sqlConnection.Close();
                    progressBar1.Value += 1;
                }

                DisplayMessage.ShowNotice("Thực hiện thành công!");
                this.Close();
                
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GrossTableForm_Load(object sender, EventArgs e)
        {
            
        }

        private void txtNewValue_Validated(object sender, EventArgs e)
        {
            //txtNewValue.Text = LookupControl.LookupControl.Instance().LookUpControl(GrossKey, GrossKey, GrossTable, GrossKey, txtNewValue.Text.Trim(), GrossKey, true);
            txtNewValue.Text = LookupControl.LookupControl.Instance().LookUpControl(GrossKey, GrossTable, txtNewValue.Text.Trim(), true);
        }

        private void txtNewValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
                //txtNewValue.Text = LookupControl.LookupControl.Instance().LookUpControl(GrossKey, GrossKey, GrossTable, GrossKey, string.Empty, GrossKey, true);
                txtNewValue.Text = LookupControl.LookupControl.Instance().LookUpControl(GrossKey, GrossTable, txtNewValue.Text.Trim(), true);
        }

        private void F5button_Click(object sender, EventArgs e)
        {
            txtNewValue.Text = LookupControl.LookupControl.Instance().LookUpControl(GrossKey, GrossTable, txtNewValue.Text.Trim(), true);
        }
    }
}
